package com.csd.activity;

import android.app.ActionBar;
import android.app.ActionBar.LayoutParams;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.csd.fragment.ListSelectFragment;
import com.csd.fragment.MapSelectFragment;
import com.csd.view.BadgeView;

/**
 * 首页
 * 
 */
public class SelectCarActivity extends FragmentActivity implements
		OnClickListener {

	private ActionBar actionBar;
	private TextView tv_map, tv_list;
	private FragmentManager fManager;
	private FragmentTransaction transaction;
	private int index = 0;
	private MapSelectFragment mapFragment = new MapSelectFragment();
	private CsdApplication csdApp;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_selectcar);
	}

	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		initView();
	}

	private void initView() {
		// TODO Auto-generated method stub
		if(csdApp == null){
			csdApp = CsdApplication.getInstance();
			csdApp.addActivity(this);
		}
		actionBar = getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(false);
		actionBar.setDisplayShowHomeEnabled(false);
		actionBar.setDisplayShowTitleEnabled(false);
		actionBar.setDisplayShowCustomEnabled(true);

		View v = LayoutInflater.from(this).inflate(R.layout.actionbar_main,
				null);
		ActionBar.LayoutParams layout = new ActionBar.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		TextView text = (TextView) v.findViewById(R.id.title);
		text.setText("公务选车");
		ImageView img = (ImageView) v.findViewById(R.id.logo);
		img.setImageDrawable(getResources().getDrawable(R.drawable.u86));
		img.setOnClickListener(this);
		actionBar.setCustomView(v, layout);


		tv_map = (TextView) findViewById(R.id.map);
		tv_list = (TextView) findViewById(R.id.list);
		tv_map.setBackgroundColor(Color.parseColor("#D2223D"));
		tv_map.setTextColor(Color.parseColor("#ffffff"));
		tv_list.setBackgroundColor(Color.parseColor("#ffffff"));
		tv_list.setTextColor(Color.parseColor("#000000"));
		tv_map.setOnClickListener(this);
		tv_list.setOnClickListener(this);
		fManager = getSupportFragmentManager();
		transaction = fManager.beginTransaction().replace(R.id.index_content,
				mapFragment);
		transaction.commit();

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.map:
			if (index == 0) {
				return;
			}
			index = 0;
			transaction = getSupportFragmentManager().beginTransaction();
			transaction.replace(R.id.index_content, mapFragment);
			transaction.commit();
			tv_map.setBackgroundColor(Color.parseColor("#D2223D"));
			tv_map.setTextColor(Color.parseColor("#ffffff"));
			tv_list.setBackgroundColor(Color.parseColor("#ffffff"));
			tv_list.setTextColor(Color.parseColor("#000000"));
			break;
		case R.id.list:
			if (index == 1) {
				return;
			}
			index = 1;
			ListSelectFragment listFragment = new ListSelectFragment();
			transaction = getSupportFragmentManager().beginTransaction();
			transaction.replace(R.id.index_content, listFragment);
			transaction.commit();
			tv_list.setBackgroundColor(Color.parseColor("#D2223D"));
			tv_list.setTextColor(Color.parseColor("#ffffff"));
			tv_map.setBackgroundColor(Color.parseColor("#ffffff"));
			tv_map.setTextColor(Color.parseColor("#000000"));
			break;
		case R.id.bt_car:
			Intent intent = new Intent(this, EnterpriseApplayActivity.class);
			startActivity(intent);
			
			break;
		case R.id.logo:
			Intent intent1 = new Intent(this, MainActivity.class);
			intent1.putExtra("num", 1);
			intent1.putExtra("flag", 0);
			startActivity(intent1);
			csdApp.exit();
			break;
		default:
			break;
		}
	}
}
